<template>
  <div :style="{ width: `${ossW || 200}px`, height: `${height}px`, margin: `0 auto`}">
    <a-carousel
      v-bind="$attrs"
      @change="onSlideChange"
      :dots="pagination"
      :autoplay="autoplay"
      :beforeChange="beforeChange"
      :afterChange="afterChange"
      arrows
    >
      <!-- 自定义上一张按钮 -->
      <div
        slot="prevArrow"
        slot-scope="props"
        class="custom-slick-arrow"
        style="left: -20px;"
      >
        <a-icon type="left" />
      </div>
      <!-- 自定义下一张按钮 -->
      <div
        slot="nextArrow"
        slot-scope="props"
        class="custom-slick-arrow"
        style="right: -20px"
      >
        <a-icon type="right" />
      </div>
      <!-- 循环渲染轮播项 -->
      <div
        v-for="(src, index) in swipperDatas"
        :key="index"
      >
        <div class="swiper-item flex items-center h-full w-full justify-center">
          <!-- 如果是视频，渲染视频播放器 -->
          <video-player
            v-if="isVideo(src)"
            :ref="`videoListRef_${index}`"
            :player-options="{ controls: true, sources: [{ src }] }"
          >
          </video-player>
          <!-- 如果不是视频且不使用懒加载，渲染viewImg组件 -->
          <viewImg
            v-else-if="(!isLazy)&&src"
            :style="{ width: `${ossW || 200}px`, height: `${height}px` }"
            style="display: flex;justify-content: center;"
            :src="src"
            :isCompress="1"
            :list-src="swipperDatas"
          >
          </viewImg>
          <!-- 如果不是视频且使用懒加载，渲染img标签 -->
          <img
            v-else-if="isLazy&&src"
            :style="{ width: `${ossW || 200}px`, height: `${height}px` }"
            loading="lazy"
            v-view-img
            :src="src"
            :isCompress="1"
            :list-src="swipperDatas"
          >
          </img>
        </div>
      </div>
    </a-carousel>
  </div>
</template>

<script>
import { isVideo } from 'utils/utils'

export default {
  name: 'CpsSwiper',
  props: {
    // 轮播项数据
    imgs: {
      type: Array,
      default: () => []
    },
    // 图片预览是否懒加载
    isLazy: {
      type: Boolean,
      default: false
    },
    // viewImg宽度
    ossW: {
      type: Number,
      default: null
    },
    // viewImg高度
    height: {
      type: [Number, String],
      default: null
    },
    // 是否显示分页器
    pagination: {
      type: Boolean,
      default: false
    },
    // 是否自动播放
    autoplay: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      // 轮播项数据
      swipperDatas: [],
      // 视频播放器引用
      videoListRef: []
    }
  },
  watch: {
    // 监听imgs变化，更新swipperDatas
    imgs: {
      handler(newVal) {
        this.swipperDatas = newVal
      },
      deep: true,
      immediate: true
    }
  },
  methods: {
    // 判断是否为视频
    isVideo,
    // 轮播项切换时的回调
    onSlideChange(current, prev) {
      this.videoListRef.forEach((video, index) => {
        if (current !== index) {
          video.pause()
        } else {
          video.play()
        }
      })
      this.$emit('slideChange', current, prev)
    },
    // 轮播项切换前的回调
    beforeChange(current, next) {
      this.$emit('beforeChange', current, next)
    },
    // 轮播项切换后的回调
    afterChange(current) {
      this.$emit('afterChange', current)
    }
  }
}
</script>

<style scoped>
.swiper-button-next.swiper-button-disabled,
.swiper-button-prev.swiper-button-disabled {
  opacity: 0.5;
  pointer-events: unset;
}
</style>
<style lang="less" scoped>
.ant-carousel .slick-arrow.custom-slick-arrow {
  font-size: 14px;
  color: #333;
  width: 20px;
  text-align: center;
  height: 40px;
  background-color: rgba(255, 255, 255, 0.6);
  /* opacity: 0.3; */
}

.ant-carousel .custom-slick-arrow:before {
  display: none;
}

.ant-carousel .custom-slick-arrow:hover {
  opacity: 0.5;
}

.custom-slick-arrow.slick-arrow.slick-prev {
  z-index: 9;
}

</style>
